<template>
  <div>
    <el-card class="box-card">
      <div slot="header" class="clearfix" style="text-align: center;line-height: 30px">
        <h3>添加角色</h3>
      </div>

      <div class="text item">
        <el-form :model="ruleForm" ref="ruleForm" label-width="100px" class="demo-ruleForm">

          <el-row>
            <el-col :span="12"><div class="grid-content bg-purple">
              <el-form-item label="角色名称" prop="name">
                <el-input v-model="ruleForm.name"></el-input>
              </el-form-item>
            </div></el-col>
          </el-row>
          <br><br>

          <el-row>
            <el-col :span="24"><div class="grid-content bg-purple">
              <el-form-item label="拥有权限">
                <el-checkbox-group v-model="ruleForm.type">
                  <div v-for="item in ruleForm.rolelist" :key="item.id" style="float: left;margin-left: 40px">
                    <el-checkbox :label="item.powerName" name="type"></el-checkbox>
                  </div>
                </el-checkbox-group>
              </el-form-item>
            </div></el-col>
          </el-row>
          <br><br>

          <el-row>
            <div style="text-align: center">
              <el-form-item>
                <el-button type="primary" @click="add">添加</el-button>
              </el-form-item>
            </div>
          </el-row>
        </el-form>
      </div>
    </el-card>
  </div>
</template>

<script>
export default {
  name: "AddRole",
  data(){
    return{
      ruleForm:{
        name:'',
        rolelist:[],
        type:[],
        power:''
      }
    }
  },
  mounted() {
    this.init()
  },
  methods:{
    init(){
      let url = 'apj/power/selectAllPower'
      this.$axios.get(url).then(r=>{
        this.ruleForm.rolelist = r.data
      })
    },
    add(){
      let url = 'apj/role/insertRole'
      let fd = new FormData
      fd.append("roleName",this.ruleForm.name)
      fd.append("powerList",this.ruleForm.type)
      this.$axios.post(url,fd).then(r=>{
        this.$message.success(r.data)
        this.$emit("init")
        this.$emit("close")
      })
    }
  }
}
</script>

<style scoped>

</style>